import * as Api from "../../api/invoice";
const app = getApp();
const aa = [
  {
    number: 1,
    checked: false,
    attraction_name: "苏州拙政园景区",
    device_number: "E3070A01",
    rental_start_time: "2021-04-09  08:00:00",
    use_duration: "3小时16分40秒",
    use_status: "",
    is_free: false,
    amount: "55",
  },
  {
    number: 2,
    checked: false,
    attraction_name: "苏州甪直古镇",
    device_number: "E3070A02",
    rental_start_time: "2021-04-09  08:00:00",
    use_duration: "3小时16分40秒",
    use_status: "",
    is_free: false,
    amount: "100",
  },
  {
    number: 3,
    checked: false,
    attraction_name: "南京博物院",
    device_number: "E3070A03",
    rental_start_time: "2021-04-09  08:00:00",
    use_duration: "3小时16分40秒",
    use_status: "",
    is_free: false,
    amount: "60",
  },
];
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    total_page: "",
    current_page: 1,
    count: 0,
    size: 10,
    loading: false, // 加载中状态
    noMoreData: false, // 是否有更多数据
    toastInvoice: "../../images/toast-invoice.png",
    isCheckedAll: false,
    orderLength: 0,
    amountAll: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetchData();
    wx.removeStorageSync("checkedList");

    // 临时方案
    // const checkedListStorage = wx.getStorageSync("checkedList")
    //   ? wx.getStorageSync("checkedList")
    //   : [];
    // console.log("checkedListStorage", checkedListStorage);
    // const list = this.data.list;
    // if (checkedListStorage.length > 0) {
    //   list.forEach((item) => {
    //     console.log(item);

    //     if (
    //       checkedListStorage.some((i) => i.number === item.number && i.checked)
    //     ) {
    //       item.checked = true;
    //     }
    //   });
    // }
    // this.setData({
    //   list,
    // });
  },

  fetchData() {
    if (this.data.loading || this.data.noMoreData) return;

    this.setData({ loading: true });

    Api.getNotInvoicedOrders().then((res) => {
      const { total_page, current_page, count, size } = res.paging;
      const list = res.list_data.map((item) => {
        return { ...item, checked: false };
      });
      console.log("list", list);
      /**
       * @description: 匹配已经选中的数据
       * @return {*}
       */
      const checkedListStorage = wx.getStorageSync("checkedList")
        ? wx.getStorageSync("checkedList")
        : [];
      console.log("checkedListStorage", checkedListStorage);

      if (checkedListStorage.length > 0) {
        list.forEach((item) => {
          const found = checkedListStorage.find(
            (i) => i.number === item.number
          );
          if (found) {
            item.checked = found.checked;
          }
        });
      }

      this.setData({
        list,
        total_page,
        current_page,
        count,
        size,
      });
      this.checkAllSelected();
    });
  },
  onReachBottom() {
    // 判断是否还有更多数据可以加载
    if (this.data.hasMoreData) {
      this.setData({ current_page: this.data.current_page + 1 }); // 页数加1
      this.fetchData(); // 加载下一页数据
    } else {
      wx.showToast({
        title: "没有更多数据了",
        icon: "none",
      });
    }
  },

  onChangeCard(e) {
    const index = e.currentTarget.dataset.index; // 获取当前项的index
    const list = this.data.list;
    // 切换 checked 状态
    list[index].checked = !list[index].checked;

    const checkedList = list.filter((item) => item.checked);
    console.log("checkedList", checkedList);

    wx.setStorageSync("checkedList", checkedList);
    // 更新页面数据
    this.setData({
      list,
      orderLength: checkedList.length,
      amountAll: parseFloat(
        checkedList
          .reduce((sum, item) => sum + parseFloat(item.amount), 0)
          .toFixed(2)
      ), // 保留两位小数,
    });

    // 检查是否全部选中
    this.checkAllSelected();
  },

  // 检查是否已经全选
  checkAllSelected() {
    const isCheckedAll = this.data.list.every((item) => item.checked);
    this.setData({
      isCheckedAll,
    });
  },
  /**
   * @description: 全选
   * @param {*} e
   * @return {*}
   */
  onChangeAll(e) {
    console.log(e.detail);
    const newList = this.data.list.map((item) => {
      return { ...item, checked: e.detail };
    });
    wx.setStorageSync("checkedList", newList);

    const amountAll = parseFloat(
      newList.reduce((sum, item) => sum + parseFloat(item.amount), 0).toFixed(2)
    );

    this.setData({
      isCheckedAll: e.detail,
      list: newList,
      orderLength: newList.length,
      amountAll,
    });
  },
  /**
   * @description: 下一步
   * @return {*}
   */
  onNext() {
    console.log("111");
    wx.navigateTo({ url: "/pages/invoiceSubmit/invoiceSubmit" });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
